//
//  EditPasswordViewController.m
//  MemberProject
//
//  Created by macbook on 15/4/16.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "EditPasswordViewController.h"
#import "UserInfoManager.h"
#import "ModelUser.h"
@interface EditPasswordViewController ()

@end

@implementation EditPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"修改密码";
    [self setupView];
    [self setupNavigation];
}
-(void)setupView
{
    self.oldPdField.layer.borderWidth = 0.3;
    self.oldPdField.layer.cornerRadius = 5;
    self.oldPdField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    self.nPdField.layer.borderWidth = 0.3;
    self.nPdField.layer.cornerRadius = 5;
    self.nPdField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    self.confirmField.layer.borderWidth = 0.3;
    self.confirmField.layer.cornerRadius = 5;
    self.confirmField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelEdit)];
    [self.view addGestureRecognizer:tap];
}
-(void)cancelEdit
{
    [self.oldPdField resignFirstResponder];
    [self.nPdField resignFirstResponder];
    [self.confirmField resignFirstResponder];
}
-(void)setupNavigation
{
    UIButton *finishBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 25)];
    [finishBtn setTitle:@"完成" forState:0];
    [finishBtn addTarget:self action:@selector(finishAction:) forControlEvents:64];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:finishBtn];
}
-(void)confirm
{
    NSString *oldStr = [Utils md5:self.oldPdField.text];
    NSString *newStr = [Utils md5:self.nPdField.text];
    
    NSString *volunteerId = [UserInfoManager sharedManager].modelMser.voluteerId;
    if (volunteerId) {
        [SVProgressHUD showWithStatus:@"修改中" maskType:SVProgressHUDMaskTypeBlack];
        NSDictionary *parameters = @{@"token":@"sdfsadfsadfasdf",@"module":@"Voluteer",@"method":@"resetPwd",@"requestId":[NSNull new],@"requestParam":@[volunteerId,oldStr,newStr]};
        
        
        [Utils requestDic:parameters andRusult:^(id result) {
            if ([result isKindOfClass:[NSDictionary class]])
            {
                id responseData = result[@"responseData"];
                if ([responseData isKindOfClass:[NSNumber class]]) {
                    NSNumber *num = responseData;
                    if (num.intValue==0) {
                        [self popMyViewController];
                    }else
                    {
                        [SVProgressHUD showErrorWithStatus:@"所填的旧密码错误" maskType:SVProgressHUDMaskTypeBlack];
                    }
                }else
                {
                    [SVProgressHUD showErrorWithStatus:@"所填的旧密码错误" maskType:SVProgressHUDMaskTypeBlack];
                }
                
            }else
            {
                [SVProgressHUD showErrorWithStatus:@"网络错误" maskType:SVProgressHUDMaskTypeBlack];
            }
        } andError:^(id result) {
            
            [SVProgressHUD showErrorWithStatus:@"网络错误" maskType:SVProgressHUDMaskTypeBlack];
        }];
    }
}
-(void)popMyViewController
{
    [SVProgressHUD showSuccessWithStatus:@"修改成功" maskType:SVProgressHUDMaskTypeBlack];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
    });
    
    
}
-(void)finishAction:(UIButton *)sender
{
    if (self.oldPdField.text.length<6) {
        [SVProgressHUD showErrorWithStatus:@"旧密码长度小于6位" maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    if (self.nPdField.text.length<6) {
        [SVProgressHUD showErrorWithStatus:@"新密码长度小于6位" maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    if (self.confirmField.text.length<6) {
        [SVProgressHUD showErrorWithStatus:@"确认密码长度小于6位" maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    if (![self.nPdField.text isEqualToString:self.confirmField.text]) {
        [SVProgressHUD showErrorWithStatus:@"密码不一致" maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    
    [self confirm];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
